package com.epfresh.pickerview.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.aigestudio.wheelpicker.widget.curved.WheelDatePicker;
import com.epfresh.supplier.R;

import com.aigestudio.wheelpicker.core.AbstractWheelPicker;
import com.aigestudio.wheelpicker.core.IWheelPicker;
import com.epfresh.api.utils.T;

/**
 * @author AigeStudio 2015-12-08
 */
public class MainDialog extends Dialog implements View.OnClickListener {
    private View root;
    private ViewGroup container;
    private Button rButton,lButton;
    private IWheelPicker picker;
    private String data;

    private Context mContext;
    public MainDialog(Context context) {
        super(context);
        mContext = context;
        padding = mContext.getResources().getDimensionPixelSize(R.dimen.WheelPadding);
        textSize = mContext.getResources().getDimensionPixelSize(R.dimen.TextSizeLarge);
        itemSpace = mContext.getResources().getDimensionPixelSize(R.dimen.ItemSpaceLarge);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setCanceledOnTouchOutside(true);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                container.removeAllViews();
            }
        });
/*      root = getLayoutInflater().inflate(R.layout.ac_main_dialog, null);
        container = (ViewGroup) root.findViewById(R.id.main_dialog_container);
        btnObtain = (Button) root.findViewById(R.id.btn_obtain);*/
        root = getLayoutInflater().inflate(R.layout.dialog_new_layout, null);
        container = (ViewGroup) root.findViewById(R.id.content);
        container.removeAllViews();
        rButton = (Button) root.findViewById(R.id.positiveButton);
        rButton.setOnClickListener(this);
        lButton = (Button) root.findViewById(R.id.negativeButton);
        lButton.setOnClickListener(this);
    }

    @Override
    public void setContentView(View view) {
        if (view instanceof IWheelPicker) {
            picker = (IWheelPicker) view;
            picker.setOnWheelChangeListener(new AbstractWheelPicker.SimpleWheelChangeListener() {
                @Override
                public void onWheelScrollStateChanged(int state) {
                    if (state != AbstractWheelPicker.SCROLL_STATE_IDLE) {
                        rButton.setEnabled(false);
                        lButton.setEnabled(false);
                    } else {
                        rButton.setEnabled(true);
                        lButton.setEnabled(false);
                    }
                }

                @Override
                public void onWheelSelected(int index, String data) {
                    MainDialog.this.data = data;
                }
            });
        }
        container.addView(view);
        super.setContentView(root);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.positiveButton:
                T.toast(data);
                break;
            case R.id.negativeButton:
                T.toast("取消");
                break;
        }
    }


    public void setTitle(String title){
        ((TextView)root.findViewById(R.id.title)).setText(title);
    }
    public void setLeftText(String text){
        ((TextView)root.findViewById(R.id.negativeButton)).setText(text);
    }
    public void setRightText(String text){
        ((TextView)root.findViewById(R.id.positiveButton)).setText(text);
    }
    private int padding;
    private int textSize;
    private int itemSpace;
    public void showDateDialog(){
        MainDialog dialog = new MainDialog(mContext);
        WheelDatePicker wheelDatePicker = new WheelDatePicker(mContext);
        wheelDatePicker.setPadding(padding, 0, padding, 0);
        wheelDatePicker.setBackgroundColor(0xFFeeeeee);
        wheelDatePicker.setTextColor(0xFF777777);
        wheelDatePicker.setCurrentTextColor(0xFF333333);
        wheelDatePicker.setLabelColor(0xFF333333);
        wheelDatePicker.setTextSize(textSize);
        wheelDatePicker.setItemSpace(itemSpace);
        wheelDatePicker.setCurrentDate(2015, 12, 20);
        dialog.setContentView(wheelDatePicker);
        dialog.setTitle("预售时间");
        dialog.show();
    }
}